// From: http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/

vec4 PackFloat(float f)
{
    vec4 pack = vec4(1.0, 255.0, 65025.0, 160581375.0) * f;
    pack = fract(pack);
    pack -= pack.yzww * vec4(1.0/255.0, 1.0/255.0, 1.0/255.0, 0.0);
    return pack;
}

float UnpackFloat(vec4 pack)
{
    return dot(pack, vec4(1.0, 1/255.0, 1/65025.0, 1/160581375.0));
}

//float SphereLightAttenuation(float dist, float lightRadius, float lightExtent)
//{
//    // Transform dist such that dd tends to infinity as d tends to lightExtent
//    float d = dist - lightRadius;
//    float dd = d / (1 - pow(d/lightExtent, 2));
//    return 1 / pow(dd/lightRadius+1, 2);
//}

// A rearrangement of the above code that avoids divisions by zero when dist==0 or dist==lightExtent
float SphereLightAttenuation(float dist, float lightRadius, float lightExtent)
{
    float d = dist - lightRadius;
    float e = lightExtent - lightRadius;
    float d2 = d * d;
    float e2 = e * e;
    float att = max(e2-d2, 0) / (lightRadius*d*e2 + e2 - d2);
    return att*att;
}
